use chrono::{Datelike, Local};
extern crate simple_excel_writer as excel;
use excel::*;
#[no_mangle]
fn stock() -> bool {
    let current_date: String = format!("{}-{}-{}",Local::now().year(),Local::now().month(),Local::now().day());
    let mut data:String = String::from("");
    let testlink: &str = "https:push2.eastmoney.com/api/qt/clist/get?np=1&fltt=2&invt=2&cb=jQuery37100258439693714817_1746908371475&fs=m%3A0%2Bt%3A6%2Cm%3A0%2Bt%3A80%2Cm%3A1%2Bt%3A2%2Cm%3A1%2Bt%3A23%2Cm%3A0%2Bt%3A81%2Bs%3A2048&fields=f12%2Cf13%2Cf14%2Cf1%2Cf2%2Cf4%2Cf3%2Cf152%2Cf5%2Cf6%2Cf7%2Cf15%2Cf18%2Cf16%2Cf17%2Cf10%2Cf8%2Cf9%2Cf23&fid=f3&pn=1&pz=10&po=1&dect=1&ut=fa5fd1943c7b386f172d6893dbfba10b&wbp2u=%7C0%7C0%7C0%7Cweb&_=1746908371477";
    match reqwest::blocking::get(testlink).is_ok(){
        true => {
                 for i in 1..=100{//代表最多10000支股票
                    let url:&str = &format!(r"https:push2.eastmoney.com/api/qt/clist/get?np=1&fltt=2&invt=2&cb=jQue
                    ry37100258439693714817_1746908371475&fs=m%3A0%2Bt%3A6%2Cm%3A0%2Bt%3A80%2Cm%3A1%2Bt%3A2%2Cm%3A1%2Bt%3A23%2Cm%3A0%2Bt%3A81%2Bs%3A2048&fields
                    =f12%2Cf13%2Cf14%2Cf1%2Cf2%2Cf4%2Cf3%2Cf152%2Cf5%2Cf6%2Cf7%2Cf15%2Cf18%2Cf16%2Cf17%2Cf10%2Cf8%2Cf9%2Cf23&fid=f3
                    &pn={i}
                    &pz=100
                    &po=1
                    &dect=1
                    &ut=fa5fd1943c7b386f172d6893dbfba10b&wbp2u=%7C0%7C0%7C0%7Cweb&_=1746908371477");
                    let mut tempdata: String = reqwest::blocking::get(url).unwrap().text().unwrap();
                    let beta_offset: usize = tempdata.find('[').unwrap_or(tempdata.len());
                    tempdata.replace_range(..beta_offset, "");
                    let tempdata: String = tempdata.replace(|c| c == '[' || c == ']'|| c == '{' || c == '}' || c == '"' || c == ')' || c == ';', "");
                    data += &",";
                    data += &tempdata;
                    }
                    data.remove(0);
                    let mut stock: Vec<&str> = data.split(',').collect();
                    // 删除末尾的空字符串
                    if let Some(last_non_empty) = stock.iter().rposition(|s| !s.is_empty()) {
                        stock.truncate(last_non_empty + 1);
                    } else {
                        stock.clear();
                    }
                    let username:String = std::env::var_os("USERNAME").unwrap().into_string().unwrap();
                    let mut wb: Workbook = Workbook::create(format!("C:/Users/{}/Desktop/{}-A股数据.xlsx",username,current_date).as_str());
                    let mut sheet: Sheet = wb.create_sheet("Sheet0");
                    wb.write_sheet(&mut sheet, |sheet_writer| {
                    let sw: &mut SheetWriter = sheet_writer;
                    sw.append_row(row!["代码", "名称","最新价","涨跌幅%","涨跌额","成交量(手)","成交额","振幅%","最高","最低","今开","昨收","量比","换手率%","市盈率","市净率"])?;
                    for i in (1..=stock.len()).filter(|x| x % 29 == 0){
                        sw.append_row(row![&stock[i-18][4..],
                                    &stock[i-16][4..],
                                    &stock[i-28][3..],
                                    &stock[i-27][3..],
                                    &stock[i-26][3..],
                                    &stock[i-25][3..],
                                    &stock[i-24][3..],
                                    &stock[i-23][3..],
                                    &stock[i-15][4..],
                                    &stock[i-14][4..],
                                    &stock[i-13][4..],
                                    &stock[i-12][4..],
                                    &stock[i-20][4..],
                                    &stock[i-22][3..],
                                    &stock[i-21][3..],
                                    &stock[i-7][4..]
                                    ])?;
                            }
                            sw.append_row(row![()])
                            }).expect("write excel error!");
                            wb.close().expect("close excel error!");
                    return true;
                            }
        false => return false,
    }
}